package gui.mainFrame;

import gui.mainFrame.codeViewer.CodeViewer;
import gui.GUIController;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.UIManager;

/**
 * Main window of the program.
 * @author William Whitney
 */
public class MainFrame extends JFrame
{

    public static final int k_height = 700;
    public static final int k_width = 1200;
    public static final Dimension k_windowSize = new Dimension(k_width, k_height);
    public static final String k_title = "Automatic JUnit Creation Tool";
    private static final long serialVersionUID = 1L;
    private JPanel panel;
    private final GUIController guiController;
    private JPanel currEditor;
    private JPanel btnBar;
    private CodeViewer codeViewer;
    private JMenuBar menuBar;
    private final JSplitPane splitPanel;

    /**
     * Default constructor.
     * @param control provides controller for test generator logic.
     */
    public MainFrame(GUIController guiController)
    {
        this.guiController = guiController;

        //Setup frame
        this.setFrameProperties();

        //Add Panel for content
        BorderLayout borderLayout = new BorderLayout();
        this.panel = new JPanel();
        this.panel.setLayout(borderLayout);
        this.add(panel);

        //Add panel to seperate code vers all else
        this.splitPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true);
        this.panel.add(splitPanel, BorderLayout.CENTER);
        this.splitPanel.setOneTouchExpandable(true);
        this.splitPanel.setDividerSize(8);

        this.currEditor = null;
        this.btnBar = null;
        this.codeViewer = null;
    }

    /**
     * Sets properties for frame such as height, width, look, feel ect.
     */
    private void setFrameProperties()
    {
        this.setTitle(k_title);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSwingLookandFeel();
        this.setMinimumSize(k_windowSize);
        this.setPreferredSize(k_windowSize);
    }

    public void showUI()
    {
        this.setVisible(true);
    }

    public void setMenuBar(MenuBar menuBar)
    {
        if (this.menuBar != null)
        {
            this.remove(this.menuBar);
        }
        this.menuBar = menuBar;
        this.panel.add(menuBar, BorderLayout.NORTH);
    }

    public void setCodeEditor(CodeViewer codeEditor)
    {
        this.codeViewer = codeEditor;
        this.splitPanel.setLeftComponent(this.codeViewer);
        pack();

    }

    public void setEditor(JPanel editor)
    {
        this.currEditor = editor;
        this.splitPanel.setRightComponent(this.currEditor);
        this.repaint();
        editor.updateUI();

    }

    public void setButtonNavBar(JPanel buttonBar)
    {
        if (this.btnBar != null)
        {
            this.panel.remove(btnBar);
        }
        this.btnBar = buttonBar;

        this.panel.add(btnBar, BorderLayout.SOUTH);
        this.repaint();
        btnBar.updateUI();
    }

    /**
     * Sets application look and feel to default java look and feel.
     */
    private void setSwingLookandFeel()
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
}
